
import sys
sys.path.append('..')
import config
import os
import json
import Levenshtein
import matplotlib.pyplot as plt

def load_json(input_file):
    d = json.load(open(input_file, 'rb'))
    # print(','.join(d.keys()))
    return d



def parse_dict(d,text='maxjax语法糖'):
    # d = load_json(input_file)
    if text in d:
        return d[text]
    else:
        most_same_text = ''
        most_same_score = 0
        res = [0, 0]
        for k,v in d.items():
            ratio_k = Levenshtein.ratio(str(k), text)
            if ratio_k>most_same_score:
                res = v
                most_same_score = ratio_k
        return res


def scatter_test(d):
    x = []
    y = []
    for k,v in d.items():
        x.append(v[0])
        y.append(v[1])
    plt.scatter(x, y)
    plt.title('rec_points')
    plt.xlabel('x')
    plt.ylabel('y')

    # 显示图表
    plt.show()

    pass



def run():
    input_file = os.path.join(config.ROOT_PATH, 'temp/screenshot_rec.json')
    d=load_json(input_file)
    # load_json(input_file)
    res = parse_dict(d, text='maxjax语法糖')
    print(res)

    scatter_test(d)






    return res








if __name__=='__main__':
    run()


    pass

